package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Plan;
import com.itheima.service.PlanService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/plan")
public class PlanController {
    @Reference
    private PlanService planService;

    //新增
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('PERMISSION_ADD')")
    public Result add(@RequestBody Plan plan) {
        try {
            planService.add(plan);//发送请求
            return new Result(true, MessageConstant.ADD_PLAN_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PLAN_FAIL);
        }
    }


    //根据id删除检查项
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('PERMISSION_DELETE')")
    public Result delete(Integer id) {
        try {
            planService.delete(id);//发送请求
            return new Result(true, MessageConstant.DELETE_PLAN_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_PLAN_FAIL);
        }
    }

    //方案类别查询
    @RequestMapping("/findCategory")
    public List findCategory() {
        List<Map> categorys = planService.findCategory();
        List<Map> maps = new ArrayList<>();
        for (Map map : categorys) {
            HashMap<String, String> stringStringHashMap = new HashMap<>();
            stringStringHashMap.put("value", (String) map.get("category"));
            stringStringHashMap.put("label", (String) map.get("category"));
            maps.add(stringStringHashMap);
        }
        return maps;

    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody Map map) {
     return    planService.findPage(map);

    }

    //根据id查询检查项
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Plan plan = planService.findById(id);//发送请求
            return new Result(true, MessageConstant.QUERY_PLAN_SUCCESS, plan);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PLAN_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('PERMISSION_EDIT')")
    public Result edit(@RequestBody Plan plan) {
        try {
            planService.edit(plan);//发送请求
            return new Result(true, MessageConstant.EDIT_PLAN_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PLAN_FAIL);
        }
    }


}
